package view.paper;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import view.MainAplication;
import db.DAOFacade;
import dto.Paper;
import dto.Researcher;
import java.awt.Component;


public class PaperPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1295200106365303269L;
	private Paper p;
	private JTextField txtRevista = new JTextField();;
	private JTextField txtEditorial = new JTextField();;
	private JTextField txtReferencias = new JTextField();;
	private JTextField txtISSN = new JTextField();
	private JTextField txtIDCONICET = new JTextField();;
	private JTextField txtTitulo = new JTextField();;
	private JPanel txtAutores = new JPanel();
	private JTextArea txtResumen = new JTextArea();
	
	/**
	 * Create the panel.
	 */
	public PaperPanel() {
		
		JScrollPane scrollPane = new JScrollPane();
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		scrollPane.setViewportView(panel);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(30, 144, 255));
		
		JPanel panel_4 = new JPanel();
		panel_4.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
		panel_4.setBackground(Color.WHITE);
		
		JLabel label = new JLabel("Datos del Art\u00EDculo");
		label.setForeground(Color.GRAY);
		label.setFont(new Font("Tahoma", Font.BOLD, 16));
		label.setBackground(Color.BLACK);
		
		
		txtAutores.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtAutores.setBorder(new LineBorder(new Color(192, 192, 192), 0));
		txtAutores.setBackground(Color.WHITE);
		
		JLabel label_2 = new JLabel("Revista:");
		
		
		txtRevista.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtRevista.setColumns(10);
		txtRevista.setBorder(new LineBorder(new Color(192, 192, 192), 0));
		txtRevista.setBackground(Color.WHITE);
		
		JLabel label_3 = new JLabel("Editorial:");
		
		
		txtEditorial.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtEditorial.setColumns(10);
		txtEditorial.setBorder(new LineBorder(new Color(192, 192, 192), 0));
		txtEditorial.setBackground(Color.WHITE);
		
		JLabel label_4 = new JLabel("Referencias:");
		
		
		txtReferencias.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtReferencias.setColumns(10);
		txtReferencias.setBorder(new LineBorder(new Color(192, 192, 192), 0));
		txtReferencias.setBackground(Color.WHITE);
		
		JLabel label_5 = new JLabel("ISSN:");
		
		
		txtISSN.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtISSN.setColumns(10);
		txtISSN.setBorder(new LineBorder(new Color(192, 192, 192), 0));
		txtISSN.setBackground(Color.WHITE);
		
		JLabel label_6 = new JLabel("Resumen:");
		txtResumen.setLineWrap(true);
		txtResumen.setWrapStyleWord(true);
		txtResumen.setAutoscrolls(false);
		
		
		txtResumen.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtResumen.setBorder(new LineBorder(new Color(192, 192, 192), 0));
		
		JButton button = new JButton("Guardar Cambios");
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				guardarDatos();
			}
		});
		button.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JLabel label_7 = new JLabel("Identificador CONICET:");
		
		
		txtIDCONICET.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtIDCONICET.setEditable(false);
		txtIDCONICET.setColumns(10);
		txtIDCONICET.setBorder(new LineBorder(new Color(192, 192, 192), 0));
		txtIDCONICET.setBackground(Color.WHITE);
		
		JButton button_1 = new JButton("Deshacer Cambios");
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				actualizarDatos();
			}
		});
		button_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JPanel panel_2 = new JPanel();
		panel_2.setBackground(Color.WHITE);
		
		JPanel panel_5 = new JPanel();
		panel_5.setBackground(Color.WHITE);
		txtTitulo.setAutoscrolls(false);
		
		
		txtTitulo.setText("-");
		txtTitulo.setHorizontalAlignment(SwingConstants.CENTER);
		txtTitulo.setFont(new Font("Tahoma", Font.BOLD, 14));
		txtTitulo.setEditable(false);
		txtTitulo.setColumns(10);
		txtTitulo.setBorder(new LineBorder(new Color(192, 192, 192), 0));
		txtTitulo.setBackground(Color.WHITE);
		GroupLayout gl_panel_5 = new GroupLayout(panel_5);
		gl_panel_5.setHorizontalGroup(
			gl_panel_5.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panel_5.createSequentialGroup()
					.addContainerGap()
					.addComponent(txtTitulo, GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel_5.setVerticalGroup(
			gl_panel_5.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panel_5.createSequentialGroup()
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(txtTitulo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
		);
		panel_5.setLayout(gl_panel_5);
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addComponent(panel_5, GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addComponent(panel_5, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel_2.setLayout(gl_panel_2);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBackground(new Color(30, 144, 255));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel_4, GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE)
					.addGap(18))
				.addComponent(panel_3, GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
				.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
				.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_4, GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
					.addContainerGap())
		);
		ImageIcon icono = new ImageIcon("atras.png");
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGap(0, 707, Short.MAX_VALUE)
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGap(0, 29, Short.MAX_VALUE)
		);
		panel_1.setLayout(gl_panel_1);
		
		JLabel labelAutores = new JLabel("Autor/es:");
		
		JButton button_2 = new JButton("<< Volver");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				MainAplication.window.showResearcher();
			}
		});
		button_2.setIcon(icono);
		
		
		button_2.setHorizontalAlignment(SwingConstants.LEFT);
		
		GroupLayout gl_panel_4 = new GroupLayout(panel_4);
		gl_panel_4.setHorizontalGroup(
			gl_panel_4.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addGap(81)
					.addComponent(button, GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
					.addGap(128)
					.addComponent(button_1, GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
					.addGap(56))
				.addGroup(gl_panel_4.createSequentialGroup()
					.addGap(37)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(label_6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(label_5, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(label_4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(label_3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(label_2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(labelAutores, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(label_7, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
					.addGap(72)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addComponent(txtAutores, GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(14)
							.addComponent(txtIDCONICET, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE))
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(16)
							.addComponent(txtRevista, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE))
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(16)
							.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
								.addComponent(txtResumen, Alignment.TRAILING)
								.addComponent(txtISSN, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
								.addComponent(txtEditorial, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
								.addComponent(txtReferencias, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE))))
					.addGap(10))
				.addGroup(gl_panel_4.createSequentialGroup()
					.addContainerGap()
					.addComponent(label, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGap(387)
					.addComponent(button_2, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE))
		);
		gl_panel_4.setVerticalGroup(
			gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addContainerGap()
							.addComponent(label))
						.addComponent(button_2, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(23)
							.addComponent(label_7))
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(18)
							.addComponent(txtIDCONICET, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)))
					.addGap(18)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addComponent(txtAutores, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addGap(18))
						.addGroup(gl_panel_4.createSequentialGroup()
							.addComponent(labelAutores)
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(5)
							.addComponent(label_2))
						.addGroup(gl_panel_4.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtRevista, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)))
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(23)
							.addComponent(label_3))
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(18)
							.addComponent(txtEditorial, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(23)
							.addComponent(label_4))
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(18)
							.addComponent(txtReferencias, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)))
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(23)
							.addComponent(label_5))
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(18)
							.addComponent(txtISSN, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)))
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(20)
							.addComponent(label_6))
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(18)
							.addComponent(txtResumen, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(60)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addComponent(button, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
						.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
					.addGap(33))
		);
		GridBagLayout gbl_txtAutores = new GridBagLayout();
		gbl_txtAutores.columnWidths = new int[]{0, 0};
		gbl_txtAutores.rowHeights = new int[]{0, 0, 0};
		gbl_txtAutores.columnWeights = new double[]{2.0, Double.MIN_VALUE};
		gbl_txtAutores.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		txtAutores.setLayout(gbl_txtAutores);
		
		panel_4.setLayout(gl_panel_4);
		panel.setLayout(gl_panel);
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 569, Short.MAX_VALUE)
					.addGap(0))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 567, Short.MAX_VALUE)
		);
		setLayout(groupLayout);
		
		incializarDatos();
	}
	
	private void incializarDatos() {
		txtTitulo.setText("-");
		txtIDCONICET.setText("-");
		txtRevista.setText("-");
		txtEditorial.setText("-");
		txtReferencias.setText("-");
		txtISSN.setText("-");
		txtResumen.setText("-");
		txtAutores.removeAll();
	}

	private void actualizarDatos() {
		incializarDatos();
		if (p!=null){
			txtTitulo.setText(p.getTitle());
			txtIDCONICET.setText(String.valueOf(p.getID_CONICET()));
			
			LinkedList<Researcher> autores = p.getAuthors();
			for (int i = 0 ; i < autores.size() ; i++){
				Researcher a = autores.get(i);
				JButton btnDirector = new ResearcherButton(a);
				btnDirector.setHorizontalAlignment(SwingConstants.LEADING);
				btnDirector.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
					}
				});
				btnDirector.setFont(new Font("Tahoma", Font.PLAIN, 13));
				btnDirector.setBorder(new LineBorder(new Color(192, 192, 192), 0));
				btnDirector.setBackground(Color.WHITE);
				
				btnDirector.setForeground(Color.BLACK);
				btnDirector.setBackground(Color.WHITE);
				Border line = new LineBorder(Color.WHITE);
				Border margin = new EmptyBorder(5, 15, 5, 15);
				Border compound = new CompoundBorder(line, margin);
				btnDirector.setBorder(compound);
				
				GridBagConstraints gbc_btnNewButton2 = new GridBagConstraints();
				gbc_btnNewButton2.anchor = GridBagConstraints.NORTH;
				gbc_btnNewButton2.fill = GridBagConstraints.HORIZONTAL;
				gbc_btnNewButton2.gridx = 0;
				gbc_btnNewButton2.gridy = i;
				txtAutores.add(btnDirector, gbc_btnNewButton2);
			}
			txtRevista.setText(p.getMagazine());
			txtEditorial.setText(p.getEditorial());
			txtReferencias.setText(p.getReferences());
			txtISSN.setText(p.getIsn());
			txtResumen.setText(p.getResume());
		}
		
	}
	
	private void guardarDatos() {
		p.setMagazine(txtRevista.getText());
		p.setEditorial(txtEditorial.getText());
		p.setReferences(txtReferencias.getText());
		p.setIsn(txtISSN.getText());
		p.setResume(txtResumen.getText());
		DAOFacade db = new DAOFacade();
		db.guardarArticulo(p);
	}

	public void setPaper(Paper p){
			this.p = p;
			actualizarDatos();
	}
	
	private static class ResearcherButton extends JButton{
		
		private final ActionListener listener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainAplication.window.showResearcher(r, false);
			}
		};
		
		private Researcher r;
		private ResearcherButton(Researcher r){
			super();
			if (r != null){
				this.r = r;
				this.setText(r.getName());
				this.addActionListener(listener);
			}
		}
	}
}
